package rpc

import (
	"fmt"
	"time"

	myactor "gitee.com/nggs/actor"
	"gitee.com/nggs/protoactor-go/actor"
)

func RequestFuture(pid *actor.PID, send IMessage, timeout time.Duration) (IMessage, error) {
	f := myactor.RootContext.RequestFuture(pid, send, timeout)
	iRecv, err := f.Result()
	if err != nil {
		return nil, err
	}
	recv, ok := iRecv.(IMessage)
	if !ok {
		return nil, fmt.Errorf("recv msg %#v is not a IMessage", recv)
	}
	return recv, nil
}

type IPromiseManager interface {
	ExecutePromise(promise *Promise)
	CheckAndRemovePromiseTimer(timerID myactor.TimerID, timerTag myactor.TimerID)
	PromiseNum() int
}

func NewPromise() *Promise {
	p := &Promise{
		PromiseContext:  &PromiseContext{},
		executeTime:     time.Now(),
		timeoutDuration: defaultPromiseTimeoutDuration,
	}
	return p
}
